package com.xinzhitong.www.controller;

import com.xinzhitong.www.exception.PostException;
import com.xinzhitong.www.exception.comment.EmptyCommentException;
import com.xinzhitong.www.exception.company.CompanyIdInvalidException;
import com.xinzhitong.www.exception.picture.PictureEmptyException;
import com.xinzhitong.www.pojo.Company;
import com.xinzhitong.www.service.CompanyService;
import com.xinzhitong.www.utils.Message;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 公司控制器
 *
 * @author 徐鑫
 */
@CrossOrigin
@RestController
@RequestMapping("company")
public class CompanyController {

    @Resource
    CompanyService companyService;

    /**
     * 获取指定公司下的岗位
     *
     * @param companyId 公司id
     * @return 岗位列表
     */
    @RequestMapping(value = "posts", method = RequestMethod.GET)
    public Message postsOfCompanyOf(
            @RequestParam("companyId") Integer companyId,
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize
    ) {
        String msg;
        try {
            return Message.createSuc(companyService.getPostsOfCompanyOf(companyId, pageNo, pageSize));
        } catch (PostException e) {
            e.printStackTrace();
            msg = "该公司暂无岗位";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取指定公司的信息
     *
     * @param companyId 公司id
     * @return 公司信息
     */
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public Message introductionOfCompanyOf(@RequestParam("companyId") Integer companyId) {
        String msg;
        try {
            Company company = companyService.getCompanyInfo(companyId);
            return Message.createSuc(company);
        } catch (CompanyIdInvalidException e) {
            e.printStackTrace();
            msg = "公司不存在";
        }
        return Message.createErr(msg);
    }


    /**
     * 获取指定公司的图片和评分
     *
     * @param companyId 公司id
     * @return 公司图片及评分
     */
    @RequestMapping(value = "pictureAndStars", method = RequestMethod.GET)
    public Message pictureAndStarsOfCompanyOf(@RequestParam("companyId") Integer companyId) {
        String msg;
        try {
            Map<String, Object> res = new HashMap<>();
            res.put("stars", companyService.getCompanyInfo(companyId).getStar());
            res.put("pictures", companyService.getCompanyPictures(companyId));
            return Message.createSuc(res);
        } catch (CompanyIdInvalidException e) {
            e.printStackTrace();
            msg = "公司不存在";
        } catch (PictureEmptyException e) {
            e.printStackTrace();
            msg = "暂无图片";
        }
        return Message.createErr(msg);
    }

    /**
     * 评论公司
     *
     * @param text 评论文本
     * @return 响应信息
     */
    @RequestMapping(value = "commentOfCompany", method = RequestMethod.PUT)
    public Message commentOfCompany(
            @RequestParam("companyId") Integer companyId,
            @RequestParam("text") String text
    ) {
        String msg = "评论成功";
        try {
            companyService.commentCompanyOf(companyId, text);
            return Message.createSuc(msg);
        } catch (CompanyIdInvalidException e) {
            e.printStackTrace();
            msg = "公司已失效";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取公司评论
     *
     * @param companyId 公司id
     * @param pageNo    页码
     * @param pageSize  页大小
     * @return 公司评论列表
     */
    @RequestMapping(value = "commentsOfCompany", method = RequestMethod.GET)
    public Message commentsOfCompany(
            @RequestParam("companyId") Integer companyId,
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize
    ) {
        String msg;
        try {
            return Message.createSuc(companyService.getCommentsOf(companyId, pageNo, pageSize));
        } catch (EmptyCommentException e) {
            e.printStackTrace();
            msg = "暂无更多评论";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取企业类型
     *
     * @return 企业类型
     */
    @ResponseBody
    @RequestMapping(value = "companyTypes", method = RequestMethod.GET)
    public Message companyTypes() {
        return Message.createSuc(companyService.getCompanyType());
    }


    /**
     * 获取所属行业枚举类
     *
     * @return 所属行业列表
     */
    @ResponseBody
    @RequestMapping(value = "industry", method = RequestMethod.GET)
    public Message industry() {
        return Message.createSuc(companyService.getIndustry());
    }

    /**
     * 获取合作企业概览
     *
     * @return 概览
     */
    @RequestMapping(value = "companiesView", method = RequestMethod.GET)
    public Message companiesView(
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize
    ) {
        return Message.createSuc(companyService.getCompanyView(pageNo, pageSize));
    }

}
